package com.simoniu.scalademo.sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.SparkSession

/**
 * 需求：使用sql操作DataFrame
 * Created by simoniu
 */
object DataFrameSqlScalaDemo {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setMaster("local")
    //创建SparkSession对象，里面包含SparkContext和SqlContext
    val sparkSession = SparkSession.builder()
      .appName("DataFrameSqlScala")
      .config(conf)
      .getOrCreate()

    val stuDf = sparkSession.read.json("D:\\uploadFiles\\students.json")
    //将DataFrame注册为一个临时表
    stuDf.createOrReplaceTempView("student")

    //使用sql查询临时表中的数据
    sparkSession.sql("select gender,count(*) as num from student group by gender")
      .show()
    sparkSession.stop()
  }
}
